import { load , isLogin} from "/admin/util/LoadView.js";
load('newslist');
const { createEditor, createToolbar } = window.wangEditor

//获取id
let updateid = new URL(location.href).searchParams.get('id')
let content=''
let cover = ''
async function render(){
    let res = await fetch(`http://localhost:3000/news/${updateid}`).then(res => res.json())
    console.log(res)
    title.value = res.title
    editor.setHtml(res.content)
    category.value = res.category
    cover = res.cover
}
render()

const editorConfig = {
    placeholder: 'Type here...',
    onChange(editor) {
      const html = editor.getHtml()
      content = html
    //   console.log('editor content', html)
      // 也可以同步到 <textarea>
    }
}

const editor = createEditor({
    selector: '#editor-container',
    html: '<p><br></p>',
    config: editorConfig,
    mode: 'default', // or 'simple'
})

const toolbarConfig = {}

const toolbar = createToolbar({
    editor,
    selector: '#toolbar-container',
    config: toolbarConfig,
    mode: 'default', // or 'simple'
})


editnewsform.onsubmit = async function (e) {
    e.preventDefault();

    // console.log(cover)
    await fetch(`http://localhost:3000/news/${updateid}`, {
        method: 'PATCH',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            title: title.value,
            content: content,
            category: category.value,
            cover
        })
    }).then(res => res.json())

    location.href = '/admin/views/newsManage/newsList/index.html'
}
photofile.onchange = function (evt) {
    let reader = new FileReader()
    reader.readAsDataURL(evt.target.files[0])
    reader.onload = function (e) {
        cover = e.target.result
    }
}
